$(function () {
    function getUrlParam(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return decodeURI(r[2]); return null;
    }
    //地址


    var address='http://yhapp.cqmjk.com';
    var shopDescription;
    //默认选择型号
    var  modelName;
    //默认选中颜色
    var colorName;
    //用户信息请求
    var contactPhone;
    //是否收藏
    var collectionStatus;
    //获取的商品id
    var shopID=getUrlParam('commodityId');
    //选择后的商品id
    var selectShopId;
    //userID
    var userID=getUrlParam('userId');
    //商品type
    var shopType=getUrlParam('type');
    //商品库存;
    var stock;
    //token
    var token=getUrlParam('token');
    //version
    var version=getUrlParam('version');
    //phoneModel
    var phoneModel=getUrlParam('phoneModel');
    //systemVersion
    var systemVersion=getUrlParam('systemVersion');
    //phoneType
    var phoneType=getUrlParam('phoneType');



    //获取url
    $.ajax({
        url:address+'/api/1/findbycommodityid',

        data:{
            userId:userID,
            commodityId:shopID,
            token:token,
            version:version,
            phoneModel:phoneModel,
            systemVersion:systemVersion,
            phoneType:phoneType,
            type:shopType
        },
        async:false,
        dataType:'json',
        success:function (result) {
            console.log(result);
            collectionStatus=result.value.collectionStatus;
            var shop=result.value.shopCommodity;
            stock=shop.totalNum;
            console.log(stock)
            var shopDes=result.value.shopDescription;
            contactPhone=result.value.contactPhone;
            shopID=shop.id;
            modelName=shop.modelName;
            colorName=shop.colorName;
            $('.shopCarHeader>div:eq(0) img').attr('src',address+shop.coverUrl);
            $('.shopCarHeader>div:eq(1) p:eq(0)').text(shop.name);
            $('.shopCarHeader>div:eq(1) p:eq(1)').text('￥'+shop.price);
            $('.shop_info').attr('number',shop.number);
            $('.goods_show img').attr('src',address+shop.coverUrl);
            $('.shop_name').text(shop.name);
            $('.shop_price').text('￥'+shop.price);
            //商品说明
            var shop_explain;
            var lens=shop.lableName.length;
            if(shop.lableName.substring(lens-1)==','){
                var newstrs=shop.lableName.substring(0,shop.lableName.length-1);
                shop_explain=newstrs.split(',');
            }
            var shop_explainlis='';
            for(var i=0;i<shop_explain.length;i++){
                if(i>4){
                    break;
                }else{
                    shop_explainlis+='<li>'+shop_explain[i]+'</li>';
                }
            }
            //商品说明弹框
            var shopDesLis='';
            for(var z in shopDes){
                shopDesLis+='<li>'+shopDes[z]['description']+'</li>'
                $('.shopExplainMiddle ul').html(shopDesLis)
            }
            $('.shop_info ul').html(shop_explainlis);


            //产品参数
            var parameterTable=$('.parameter table tbody');
            for(var n in shop){
                if(n=='categoryName'){
                    if(shop[n]==''){
                        parameterTable.append('<tr><td>分类</td><td>'+shop[n]+'</td></tr>')
                    }
                }else if(n=='alias'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>别名</td><td>'+shop[n]+'</td></tr>')
                    }
                } else if(n=='position'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>部位</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='shape'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>形状</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='areaProvinceName'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>产地</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='machining'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>加工</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='taste'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>性味</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='meridian'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>归经</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='effect'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>功效</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='useing'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>用法</td><td>'+shop[n]+'</td></tr>')
                    }
                }
                else if(n=='taboo'){
                    if(!shop[n]==''){
                        parameterTable.append('<tr><td>禁忌</td><td>'+shop[n]+'</td></tr>')
                    }
                }

            }
            //包装售后
            /*------------包装清单------------*/
            var arr=shop.packingList.split('，');
            var packingListlis='';
            for(var m=0;m<arr.length;m++){
                packingListlis+='<li>'+arr[m]+'</li>';
            }
            $('.packingList').html(packingListlis)
            /*------------售后服务------------*/
            $('.afterSalesService ul').html('<li>'+shop.afterSalesService+'</li>')

        },error:function () {
            console.log('加载失败')
        }
    })
    //正品保证
    $('.shop_info>div>div:eq(1)').on('click',function(){
        $('.shopExplain').slideDown(500);
        $('.wrapper').show();
        $('.wrapper').css({
            height:$(document).height()+'px'
        })
        $('html').css({
            'overflow':'hidden'
        })
        $(document.body).css({
            'overflow':'hidden'
        })
        $('.close').click(function () {
            $('.shopExplain').slideUp(500);
            $('.wrapper').hide();
            $('html').css({
                'overflow':'visible'
            })
            $(document.body).css({
                'overflow':'visible'
            })
        })
    })
    //加入购物车
    $('.addShopcar').on('click',function () {
        if(token=='f47ae89213514fa79dad5107662e9c24'){
            var test2={
                subjectImp1:function(){
                    SubjectJavaScriptImpl.loginOUT();
                }
            };
            test2.subjectImp1();
        }else{
            if(stock<=0){
                $(document.body).append('<div class="cancelCollect"><p>库存不足，掌柜的正在备货，请稍等~ </p></div>');
                $('.cancelCollect').css({
                    left:($(document).innerWidth()-$('.cancelCollect').innerWidth())/2+'px',
                });
                setTimeout(function () {
                    $('.cancelCollect').remove()
                },1000)
            }else{
                $.ajax({
                    url:address+'/api/1/cart/addshoppingcart',
                    data:{
                        amount:$('.count').text(),
                        commodityId:shopID,
                        type:shopType,
                        userId:userID,
                        token:token,
                        version:version,
                        phoneModel:phoneModel,
                        systemVersion:systemVersion,
                        phoneType:phoneType
                    },
                    dataType:'json',
                    success:function (result) {
                        if(result.resultCode==200){
                            $(document.body).append('<div class="cancelCollect"><p>添加购物车成功</p></div>');
                            $('.cancelCollect').css({
                                left:($(document).innerWidth()-$('.cancelCollect').innerWidth())/2+'px'
                            });
                            setTimeout(function () {
                                $('.cancelCollect').remove()
                            },1000)
                        }else if(result.resultCode==500){
                            $(document.body).append('<div class="cancelCollect"><p>添加购物车失败</p></div>');
                            $('.cancelCollect').css({
                                left:($(document).innerWidth()-$('.cancelCollect').innerWidth())/2+'px'
                            });
                            setTimeout(function () {
                                $('.cancelCollect').remove()
                            },1000)
                        }

                    },error:function () {
                        console.log('加载失败')
                    }
                })
            }
        }
    })
    //立即购买
    $('.buyNow').on('click',function () {
        if(stock<=0){
            $(document.body).append('<div class="cancelCollect"><p>库存不足，掌柜的正在备货，请稍等~ </p></div>');
            $('.cancelCollect').css({
                left:($(document).innerWidth()-$('.cancelCollect').innerWidth())/2+'px',
            });
            setTimeout(function () {
                $('.cancelCollect').remove()
            },1000)
        }else{
            if(token=='f47ae89213514fa79dad5107662e9c24'){
                var test2={
                    subjectImp1:function(){
                        SubjectJavaScriptImpl.loginOUT();
                    }
                };
                test2.subjectImp1();
            }
            var test1={
                subjectImpl:function(){
                    SubjectJavaScriptImpl.goBuyToNow($('.count').text()+','+shopID);
                }
            };
            test1.subjectImpl();
        }


    })
    var num=1;
    $('.reduce').on('click',function(){
        if(num==1){
            num=1
        }else {
            num--;
        }
        $('.count').text(num);
    })
    $('.add').on('click',function(){
        num++;
        $('.count').text(num)
    })
    //客服
    $('.custom_server').on('click',function(){
        var test1={
            subjectImpl:function(){
                SubjectJavaScriptImpl.goCallToPhone(contactPhone);
            }
        };
        test1.subjectImpl()
        if(!$(this).hasClass('selectCusSer')){
            $(this).attr('class','custom_server selectCusSer');
            $(this).find('img').attr('src','../images/mall_kefu_h.svg');
        }else {
            $(this).removeClass('selectCusSer');
            $(this).find('img').attr('src','../images/mall_kefu.svg');
        }
    })
    //收藏
    //是否收藏
    if(collectionStatus==2){
        $('.collect').find('img').attr('src','../images/mall_collect.svg');
        $('.collect').find('dd').html('收藏')
    }else if(collectionStatus==1){
        $('.collect').find('img').attr('src','../images/mall_collect_h.svg');
        $('.collect').find('dd').html('已收藏')
    }
    $('.collect').on('click',function() {
        if(token=='f47ae89213514fa79dad5107662e9c24'){
            var test2={
                subjectImp1:function(){
                    SubjectJavaScriptImpl.loginOUT();
                }
            };
            test2.subjectImp1();
        }else{
            //收藏请求
            $.ajax({
                url:address+'/api/1/usersaveorcanncelshopcollection',
                data:{
                    userId:userID,
                    commodityId:shopID,
                    token:token,
                    version:version,
                    phoneModel:phoneModel,
                    systemVersion:systemVersion,
                    phoneType:phoneType,
                    type:shopType
                },
                dataType:'json',
                success:function(result){
					
                    if(result.value==2){
					
                        $('.collect').find('img').attr('src','../images/mall_collect.svg');
                        $(document.body).append('<div class="cancelCollect"><p>取消收藏成功</p></div>');
                        $('.cancelCollect').css({
                            left:($(document).innerWidth()-$('.cancelCollect').innerWidth())/2+'px'
                        });
                        setTimeout(function () {
                            $('.cancelCollect').remove()
                        },1000)
                        $('.collect').find('dd').html('收藏')
                    }else if(result.value==1){
						
                        $('.collect').find('img').attr('src','../images/mall_collect_h.svg')
                        $('.collect').find('dd').html('已收藏')
                    }
					
                },error:function(){
                    console.log('加载失败')
                }
            })
        }
    })
    //回到顶部
    var backButton=$('#toTop');
    function backToTop() {
        $('html,body').animate({
            scrollTop: 0
        }, 800);
    }
    backButton.on('click', backToTop);

    $(window).on('scroll', function () {/*当滚动条的垂直位置大于浏览器所能看到的页面的那部分的高度时，回到顶部按钮就显示 */
        if ($(window).scrollTop() > $(window).height())
            backButton.fadeIn();
        else
            backButton.fadeOut();
    });
    $(window).trigger('scroll');/*触发滚动事件，避免刷新的时候显示回到顶部按钮*/

})